package com.ruoyi.functiontest.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.functiontest.mapper.RoadSectionDistanceTestMapper;
import com.ruoyi.functiontest.domain.RoadSectionDistanceTest;
import com.ruoyi.functiontest.service.IRoadSectionDistanceTestService;

/**
 * 主线站点间里程实测数据测试Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-25
 */
@Service
public class RoadSectionDistanceTestServiceImpl implements IRoadSectionDistanceTestService 
{
    @Autowired
    private RoadSectionDistanceTestMapper roadSectionDistanceTestMapper;

    /**
     * 查询主线站点间里程实测数据测试
     * 
     * @param id 主线站点间里程实测数据测试主键
     * @return 主线站点间里程实测数据测试
     */
    @Override
    public RoadSectionDistanceTest selectRoadSectionDistanceTestById(Long id)
    {
        return roadSectionDistanceTestMapper.selectRoadSectionDistanceTestById(id);
    }

    /**
     * 查询主线站点间里程实测数据测试列表
     * 
     * @param roadSectionDistanceTest 主线站点间里程实测数据测试
     * @return 主线站点间里程实测数据测试
     */
    @Override
    public List<RoadSectionDistanceTest> selectRoadSectionDistanceTestList(RoadSectionDistanceTest roadSectionDistanceTest)
    {
        return roadSectionDistanceTestMapper.selectRoadSectionDistanceTestList(roadSectionDistanceTest);
    }

    /**
     * 新增主线站点间里程实测数据测试
     * 
     * @param roadSectionDistanceTest 主线站点间里程实测数据测试
     * @return 结果
     */
    @Override
    public int insertRoadSectionDistanceTest(RoadSectionDistanceTest roadSectionDistanceTest)
    {
        return roadSectionDistanceTestMapper.insertRoadSectionDistanceTest(roadSectionDistanceTest);
    }

    /**
     * 修改主线站点间里程实测数据测试
     * 
     * @param roadSectionDistanceTest 主线站点间里程实测数据测试
     * @return 结果
     */
    @Override
    public int updateRoadSectionDistanceTest(RoadSectionDistanceTest roadSectionDistanceTest)
    {
        return roadSectionDistanceTestMapper.updateRoadSectionDistanceTest(roadSectionDistanceTest);
    }

    /**
     * 批量删除主线站点间里程实测数据测试
     * 
     * @param ids 需要删除的主线站点间里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteRoadSectionDistanceTestByIds(Long[] ids)
    {
        return roadSectionDistanceTestMapper.deleteRoadSectionDistanceTestByIds(ids);
    }

    /**
     * 删除主线站点间里程实测数据测试信息
     * 
     * @param id 主线站点间里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteRoadSectionDistanceTestById(Long id)
    {
        return roadSectionDistanceTestMapper.deleteRoadSectionDistanceTestById(id);
    }

    /**
     * 是否存在子节点
     * 
     * @param id 节点ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean hasChildById(Long id)
    {
        int result = roadSectionDistanceTestMapper.hasChildById(id);
        return result > 0;
    }
    
    /**
     * 导入主线站点间里程实测数据测试数据
     * 简化导入逻辑：直接按照Excel原始格式导入，通过id和parentid来确定数据
     * 
     * @param importList 导入数据列表
     * @param updateSupport 是否更新支持
     * @return 结果
     */
    @Override
    public String importRoadSectionDistanceTest(List<RoadSectionDistanceTest> importList, boolean updateSupport)
    {
        if (importList == null || importList.isEmpty())
        {
            return "导入数据为空！";
        }
        
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        
        // 简化导入逻辑：直接按照Excel原始格式导入
        for (RoadSectionDistanceTest data : importList)
        {
            try
            {
                // 基本数据验证
                if (data.getRoadName() == null) {
                    data.setRoadName("");
                }
                
                // 通过id和parentid来确定数据
                RoadSectionDistanceTest existingNode = null;
                
                // 使用新的方法通过id和parentid组合来精确匹配数据
                RoadSectionDistanceTest query = new RoadSectionDistanceTest();
                query.setId(data.getId());
                query.setParentId(data.getParentId());
                
                List<RoadSectionDistanceTest> existingNodes = roadSectionDistanceTestMapper.selectRoadSectionDistanceTestByIdAndParentId(query);
                if (existingNodes != null && !existingNodes.isEmpty()) {
                    existingNode = existingNodes.get(0);
                }
                
                if (existingNode != null) {
                    // 已存在节点，如果支持更新则更新
                    if (updateSupport) {
                        existingNode.setRoadName(data.getRoadName());
                        existingNode.setNodeName(data.getNodeName());
                        existingNode.setLeftLength(data.getLeftLength());
                        existingNode.setRightLength(data.getRightLength());
                        existingNode.setAvgLength(data.getAvgLength());
                        existingNode.setRemark(data.getRemark());
                        existingNode.setParentId(data.getParentId());
                        
                        roadSectionDistanceTestMapper.updateRoadSectionDistanceTest(existingNode);
                        successNum++;
                    } else {
                        // 不支持更新，跳过此条数据
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + ". 数据已存在，跳过：" + data.getRoadName() + " - " + data.getNodeName());
                    }
                } else {
                    // 不存在节点，创建新节点
                    RoadSectionDistanceTest newNode = new RoadSectionDistanceTest();
                    newNode.setId(data.getId()); // 使用Excel中的id值
                    newNode.setRoadName(data.getRoadName());
                    newNode.setNodeName(data.getNodeName());
                    newNode.setLeftLength(data.getLeftLength());
                    newNode.setRightLength(data.getRightLength());
                    newNode.setAvgLength(data.getAvgLength());
                    newNode.setRemark(data.getRemark());
                    newNode.setParentId(data.getParentId());
                    
                    roadSectionDistanceTestMapper.insertRoadSectionDistanceTest(newNode);
                    successNum++;
                }
                
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + ". 导入失败：";
                if (data.getRoadName() != null)
                {
                    msg += "路段名称：" + data.getRoadName();
                }
                if (data.getNodeName() != null)
                {
                    msg += "，起点/迄点：" + data.getNodeName();
                }
                failureMsg.append(msg + e.getMessage());
            }
        }
        
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            return failureMsg.toString();
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据已导入到系统。");
        }
        
        return successMsg.toString();
    }
    
    /**
     * 批量更新实测单位审核状态为0
     * 
     * @return 更新记录数
     */
    @Override
    public int updateReview1ToZero()
    {
        return roadSectionDistanceTestMapper.updateReview1ToZero();
    }
    
    /**
     * 检查所有记录的review1字段是否都为0
     * 
     * @return 结果 true 都为0 false 存在不为0的记录
     */
    @Override
    public boolean checkAllReview1IsZero()
    {
        int count = roadSectionDistanceTestMapper.checkAllReview1IsZero();
        return count == 0;
    }
    
    /**
     * 根据条件批量更新业主审核状态为0
     * 
     * @param roadSectionDistanceTest 查询条件
     * @return 更新记录数
     */
    @Override
    public int updateReview2ToZeroByCondition(RoadSectionDistanceTest roadSectionDistanceTest)
    {
        return roadSectionDistanceTestMapper.updateReview2ToZeroByCondition(roadSectionDistanceTest);
    }
    
    /**
     * 检查所有记录的review1和review2字段是否都不为空且都为0
     * 
     * @return 结果 true 都不为空且都为0 false 存在不为0或为空的记录
     */
    @Override
    public boolean checkAllReview1AndReview2AreZero()
    {
        int count = roadSectionDistanceTestMapper.checkAllReview1AndReview2AreZero();
        return count == 0;
    }
    
    /**
     * 批量更新中心审核状态为0
     * 
     * @return 更新记录数
     */
    @Override
    public int updateReview3ToZero()
    {
        return roadSectionDistanceTestMapper.updateReview3ToZero();
    }
}
